<template>
  <div class="app-container">
    <el-tabs v-model="activeName" type="card" class="elTabs">
      <el-tab-pane name="first">
        <div slot="label" class="pageHeader">操作日志</div>
      </el-tab-pane>
    </el-tabs>
    <div class="container">
      <div class="operlogManage">
        <div class="searchBar" v-show="showSearch">
          <div class="searchForm">
            <el-form
              :model="queryParams"
              ref="queryForm"
              :inline="true"
              size="small"
            >
              <el-form-item label="系统模块:" prop="title">
                <el-input
                  class="inputLength"
                  v-model.trim="queryParams.title"
                  placeholder="请输入系统模块"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="类型:" prop="businessType">
                <el-select
                  class="inputLength"
                  v-model="queryParams.businessType"
                  placeholder="操作类型"
                  clearable
                >
                  <el-option
                    v-for="dict in dict.type.sys_oper_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="状态:" prop="status">
                <el-select
                  v-model="queryParams.status"
                  placeholder="操作状态"
                  clearable
                  class="statusSelect"
                >
                  <el-option
                    v-for="dict in dict.type.sys_common_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="操作时间:">
                <el-date-picker
                  class="operDatePicker"
                  v-model="dateRange"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                ></el-date-picker>
              </el-form-item>
            </el-form>
          </div>
          <div class="searchButtons">
            <el-button class="searchButton" @click="handleQuery"
              >搜索</el-button
            >
            <el-button class="resetButton" @click="resetQuery">重置</el-button>
          </div>
        </div>
        <div class="addDeleteBar">
          <el-button
            :class="!multiple ? 'deleteButton' : 'allDeleteButton'"
            icon="el-icon-delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:operlog:remove']"
            >批量删除</el-button
          >
          <el-button
            class="addButton"
            icon="el-icon-delete"
            @click="handleClean"
            v-hasPermi="['system:operlog:remove']"
            >清空</el-button
          >
          <el-button
            class="addButton"
            icon="el-icon-upload2"
            @click="handleExport"
            v-hasPermi="['system:operlog:export']"
            >导出</el-button
          >
        </div>
        <div class="tableSelectArea">
          <div>
            <div class="iconInfo el-icon-info"></div>
            <span class="infoText">已选择</span>
            <span class="infoText infoActive" v-text="selectedNum"></span>
            <span class="infoText">项</span>
            <span class="infoButton" @click="clearSelect">清空</span>
          </div>
          <div>
            <right-toolbar
              :showSearch.sync="showSearch"
              @queryTable="getList"
            ></right-toolbar>
          </div>
        </div>
        <div class="table">
          <el-table
            ref="multipleTable"
            class="tableArea"
            v-loading="loading"
            :data="list"
            @selection-change="handleSelectionChange"
            :default-sort="defaultSort"
            @sort-change="handleSortChange"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
              label="序号"
              align="center"
              type="index"
              width="100"
            />
            <el-table-column label="系统模块" align="center" prop="title" />
            <el-table-column
              label="操作类型"
              align="center"
              prop="businessType"
            >
              <template slot-scope="scope">
                <dict-tag
                  :options="dict.type.sys_oper_type"
                  :value="scope.row.businessType"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="请求方式"
              align="center"
              prop="requestMethod"
            />
            <el-table-column
              label="操作人员"
              align="center"
              prop="operName"
              :show-overflow-tooltip="true"
              sortable="custom"
              :sort-orders="['descending', 'ascending']"
              width="100"
            />
            <el-table-column
              label="主机"
              align="center"
              prop="operIp"
              width="130"
              :show-overflow-tooltip="true"
            />
            <el-table-column label="操作状态" align="center" prop="status">
              <template slot-scope="scope">
                <dict-tag
                  :options="dict.type.sys_common_status"
                  :value="scope.row.status"
                />
              </template>
            </el-table-column>
            <el-table-column
              :show-overflow-tooltip="true"
              label="操作日期"
              align="center"
              prop="operTime"
              sortable="custom"
              :sort-orders="['descending', 'ascending']"
            >
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.operTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="left"
              width="200"
              fixed="right"
            >
              <template slot-scope="scope">
                <div class="tableButtonArea">
                  <el-button
                    class="operationButton"
                    type="text"
                    @click="handleView(scope.row, scope.index)"
                    v-hasPermi="['system:operlog:query']"
                    >详细</el-button
                  >
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="tableFooter">
          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </div>

      <!-- 操作日志详细 -->
      <el-dialog
        :visible.sync="open"
        width="700px"
        append-to-body
        class="dialogClass dialog1"
      >
        <template slot="title">
          <div class="modalTitle">
            <img
              src="../../../assets/bluelogo/drawerTitle.png"
              alt=""
              class="titleLogo"
            />操作日志详细
          </div>
        </template>
        <el-form ref="form" :model="form" label-width="100px" size="mini">
          <el-row>
            <el-col :span="12">
              <el-form-item label="操作模块："
                >{{ form.title }} / {{ typeFormat(form) }}</el-form-item
              >
              <el-form-item label="登录信息："
                >{{ form.operName }} / {{ form.operIp }}</el-form-item
              >
            </el-col>
            <el-col :span="12">
              <el-form-item label="请求地址：">{{ form.operUrl }}</el-form-item>
              <el-form-item label="请求方式：">{{
                form.requestMethod
              }}</el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="操作方法：">{{ form.method }}</el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="请求参数：">{{
                form.operParam
              }}</el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="返回参数：">{{
                form.jsonResult
              }}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="操作状态：">
                <div v-if="form.status === 0">正常</div>
                <div v-else-if="form.status === 1">失败</div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="操作时间：">{{
                parseTime(form.operTime)
              }}</el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="异常信息：" v-if="form.status === 1">{{
                form.errorMsg
              }}</el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="resetButton" @click="open = false">关 闭</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { list, delOperlog, cleanOperlog } from "@/api/system/operlog";

export default {
  name: "Operlog",
  dicts: ["sys_oper_type", "sys_common_status"],
  data() {
    return {
      activeName: "first",
      //列表选中数量
      selectedNum: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: { prop: "operTime", order: "descending" },
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        operName: undefined,
        businessType: undefined,
        status: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //清空列表选择
    clearSelect() {
      this.$refs.multipleTable.clearSelection();
    },
    /** 查询登录日志 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 操作日志类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(
        this.dict.type.sys_oper_type,
        row.businessType
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1;
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order);
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.operId);
      this.multiple = !selection.length;
      this.selectedNum = selection.length;
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.open = true;
      this.form = row;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const operIds = row.operId || this.ids;
      this.$modal
        .confirm("是否确认删除" + this.selectedNum + "条操作日志数据")
        .then(function () {
          return delOperlog(operIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$modal
        .confirm("是否确认清空所有操作日志数据项？")
        .then(function () {
          return cleanOperlog();
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("清空成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/operlog/export",
        {
          ...this.queryParams,
        },
        `operlog_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../../../assets/styles/variables.scss";
@import "../../../assets/styles/dialog.scss";
.app-container {
  padding: 0;
}
.operlogManage {
  height: 100%;
  .tableFooter {
    text-align: right;
    justify-content: flex-end;
    // margin-top: 25px;
    margin-right: 35px;
    // width: 100%;
    ::v-deep .btn-prev {
      color: #d4d3d3 !important;
      background-color: #fff;
      border: 1px solid #d4d3d3;
    }
    ::v-deep .btn-next {
      color: #d4d3d3 !important;
      background-color: #fff;
      border: 1px solid #d4d3d3;
    }
    ::v-deep .el-pager li {
      background-color: #fff;
      border: 1px solid #d4d3d3;
      font-size: 12px;
      font-family: $font-family;
      font-weight: 400;
      color: #5a5b5d;
    }
  }
}
.input {
  width: 100px;
}
.el-tabs--card > .el-tabs__header .el-tabs__nav {
  border: 0px;
}
::v-deep .el-tabs--card .el-tabs__header {
  background-color: #e4e7ed;
}
::v-deep.el-tabs--card .el-tabs__header .el-tabs__item {
  border-radius: 10px solid black;
  background-color: #fff;
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #053ef6;
  line-height: 48px;
  margin-top: 0;
}
.el-tabs--card {
  margin-left: 10px;
  border: 0px !important;
}
::v-deep.inputLength {
  width: 150px !important;
}
::v-deep.statusSelect {
  width: 130px !important;
}
::v-deep.operDatePicker {
  width: 240px !important;
}
</style>
